class BookRatingsController < ApplicationController
  # GET /book_ratings
  # GET /book_ratings.json
  def index
    
    if !current_user
      respond_to do |format|
        format.html { redirect_to log_in}
        return
      end
    end
    
    @book_ratings = BookRating.all

    respond_to do |format|
      format.html # index.html.erb
      format.json { render :json => @book_ratings }
    end
  end

  # GET /book_ratings/1
  # GET /book_ratings/1.json
  def show
    
    redirect_to_login if !current_user
    
    @book_rating = BookRating.find(params[:id])
    @book = current_book
    respond_to do |format|
      format.html # show.html.erb
      format.json { render :json => @book_rating }
    end
  end

  # GET /book_ratings/new
  # GET /book_ratings/new.json
  def new
  
    
    @book_rating = BookRating.new
    @book = Book.find_by_id(params[:book_id])
    session[:book_id] = @book.id

    if !current_user.nil?
      logger.info("AHAHAH")
    end

    respond_to do |format|
        if current_user
          format.html { render "home/index"} 
        else
          format.html # new.html.erb
          format.json { render :json => @book_rating }
        end
        return
    end
    
  end

  # GET /book_ratings/1/edit
  def edit
    @book_rating = BookRating.find(params[:id])
  end

  # POST /book_ratings
  # POST /book_ratings.json
  def create
    @book_rating = BookRating.new(params[:book_rating])
    @book_rating.user_id = current_user.id
    @book_rating.book_id = current_book.id

    respond_to do |format|
      if @book_rating.save
        format.html { redirect_to @book_rating, :notice => 'Book rating was successfully created.' }
        format.json { render :json => @book_rating, :status => :created, :location => @book_rating }
      else
        format.html { render :action => "new" }
        format.json { render :json => @book_rating.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /book_ratings/1
  # PUT /book_ratings/1.json
  def update
    @book_rating = BookRating.find(params[:id])

    respond_to do |format|
      if @book_rating.update_attributes(params[:book_rating])
        format.html { redirect_to @book_rating, :notice => 'Book rating was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render :action => "edit" }
        format.json { render :json => @book_rating.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /book_ratings/1
  # DELETE /book_ratings/1.json
  def destroy
    @book_rating = BookRating.find(params[:id])
    @book_rating.destroy

    respond_to do |format|
      format.html { redirect_to book_ratings_url }
      format.json { head :no_content }
    end
  end
end
